'''
Created on Jul 12, 2012

@author: Jeremy Tramm
'''

import pygame
import random

black = (0, 0 , 0)
white = (255, 255, 255)
red = (255, 0, 0)
blue = (0, 0, 255)

framesPerSecond = 30
playerSpeed = 6

screen_width = 700
screen_height = 400

class Block(pygame.sprite.Sprite):
    
    def __init__(self, color = black):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([20, 15])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.dx = 0
        self.dy = 0
    
    def update(self):
        self.rect.move_ip(self.dx, self.dy)
        
class Player(Block):
    
    def __init__(self):
        Block.__init__(self, blue)
        self.link0 = pygame.image.load("../images/link0.png")
        self.link1 = pygame.image.load("../images/link1.png")
        self.link2 = pygame.image.load("../images/link2.png")
        self.link3 = pygame.image.load("../images/link3.png")
        self.image = self.link0
        self.rect = self.image.get_rect()
        
    def update(self):
        Block.update(self)
        self.updateImage()
        if self.rect.x < 0:
            self.rect.x = 0
        elif self.rect.x > screen_width - self.rect.width:
            self.rect.x = screen_width - self.rect.width
        if self.rect.y < 0:
            self.rect.y = 0
        elif self.rect.y > screen_height - self.rect.height:
            self.rect.y = screen_height - self.rect.height
            
    def updateImage(self):
        if self.dx > 0:
            self.image = self.link3
        elif self.dx < 0:
            self.image = self.link2
        if self.dy > 0:
            self.image = self.link0
        elif self.dy < 0:
            self.image = self.link1
        
class PointBlock(Block):
    
    def __init__(self, dx, dy):
        Block.__init__(self)
        self.image = pygame.image.load("../images/rupee.png")
#        self.image = pygame.transform.scale(self.image, [50, 50])
        self.image.set_colorkey([120, 0, 255])
        self.rect = self.image.get_rect()
        self.dx = 2
        self.dy = 0
        
    def update(self):
        Block.update(self)
        if self.rect.x < 0:
            self.dx = -self.dx
            self.rect.x = 0
        elif self.rect.x > screen_width - self.rect.width:
            self.dx = -self.dx
            self.rect.x = screen_width - self.rect.width
        if self.rect.y < 0:
            self.dy = -self.dy
            self.rect.y = 0
        elif self.rect.y > screen_height - self.rect.height:
            self.dy = -self.dy
            self.rect.y = screen_height - self.rect.height
            
class KillBlock(Block):
    
    def __init__(self, dx, dy):
        Block.__init__(self, red)
        self.image = pygame.image.load("../images/bubble.png")
#        self.image = pygame.transform.scale(self.image, [50, 50])
        self.image.set_colorkey([0, 128, 255])
        self.rect = self.image.get_rect()
        self.dx = dx
        self.dy = dy
        
    def update(self):
        PointBlock.update(self)
        
pygame.init()

screen = pygame.display.set_mode([screen_width, screen_height])

block_list = pygame.sprite.Group()
kill_block_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()

for i in range(50):
    dx = random.randrange(-playerSpeed, playerSpeed)
    dy = random.randrange(-playerSpeed, playerSpeed)
    
    block = PointBlock(dx, dy)
    
    block.rect.x = random.randrange(screen_width)
    block.rect.y = random.randrange(screen_height)
    
    block_list.add(block)
    all_sprites_list.add(block)
    
for i in range(3):
    dx = random.randrange(1, int(.6*playerSpeed))
    dy = random.randrange(1, int(.6*playerSpeed))
    
    block = KillBlock(dx, dy)

    block.rect.x = random.randrange(int(.5*(screen_width)), screen_width)
    block.rect.y = random.randrange(int(.5*(screen_height)), screen_height)
    
    
    kill_block_list.add(block)
    all_sprites_list.add(block)
    
    
    
player = Player()
all_sprites_list.add(player)

gameMap = pygame.image.load("../images/map.jpg")
gameMap = pygame.transform.scale(gameMap, [screen_width, screen_height])

font = pygame.font.SysFont("Arial",30, False, False)
blitExample = pygame.image.load("../images/link2.png")

clock = pygame.time.Clock()

#pygame.key.set_repeat(1, 1)

score = 0

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                player.dy -= playerSpeed # player.dy -= playerSpeed == player.dy = player.dy - playerSpeed
#                player.rect.y -= playerSpeed
            if event.key == pygame.K_DOWN:
                player.dy += playerSpeed
#                player.rect.y += playerSpeed
            if event.key == pygame.K_LEFT:
                player.dx -= playerSpeed
#                player.rect.x -= playerSpeed
            if event.key == pygame.K_RIGHT:
                player.dx += playerSpeed
#                player.rect.x += playerSpeed
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                player.dy -= -playerSpeed # player.dy -= playerSpeed == player.dy = player.dy - playerSpeed
            if event.key == pygame.K_DOWN:
                player.dy += -playerSpeed
            if event.key == pygame.K_LEFT:
                player.dx -= -playerSpeed
            if event.key == pygame.K_RIGHT:
                player.dx += -playerSpeed
#    screen.fill(white)
    screen.blit(gameMap, [0, 0])
    all_sprites_list.update()
    all_sprites_list.draw(screen)
    
    scoreImage = font.render("Score: " + str(score), False, black)
    screen.blit(scoreImage, [10, 10])
    
    screen.blit(blitExample, [50, 50])
    
    pygame.display.flip() # similar to pygame.display.update()
    
    blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)
    
    if len(blocks_hit_list) > 0:
        score = score + len(blocks_hit_list)
        print(score)
        
    kill_blocks_hit_list = pygame.sprite.spritecollide(player, kill_block_list, False)
    
    if len(kill_blocks_hit_list) > 0:
        print("Oh No! You lose!")
        pygame.quit()
        exit()
        
    if len(block_list.sprites()) == 0:
        print("You won! Good Job!")
        pygame.quit()
        exit()
    
    clock.tick(framesPerSecond)
    